package com.platform.web.controller.report;

import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.module.domain.BaseCalorimeterData;
import com.platform.module.domain.CalorRecord;
import com.platform.module.service.IBaseCalorimeterDataService;
import com.platform.module.service.ICalorRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 热表统计曲线图
 *
 * @author wangqi
 * @date 2020-08-04
 */
@Controller
@RequestMapping("/report/calor")
public class CalorReportController extends BaseController {

    private String prefix = "report/calor";

    @Autowired
    private ICalorRecordService calorRecordService;
    @Autowired
    private IBaseCalorimeterDataService baseCalorimeterDataService;

    @RequiresPermissions("report:calor:view")
    @GetMapping("/{calorimeterId}")
    public String calor(@PathVariable("calorimeterId") Long calorimeterId, ModelMap mmap)
    {
        BaseCalorimeterData baseCalorimeterData = new BaseCalorimeterData();
        if(calorimeterId != 0L){
            baseCalorimeterData = baseCalorimeterDataService.getById(calorimeterId);
        }
        mmap.put("data", baseCalorimeterData);
        mmap.put("calorimeterList", baseCalorimeterDataService.list());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        mmap.put("beginTime", dateFormat.format(new Date()));
        mmap.put("endTime", dateFormat.format(new Date()));
        return prefix + "/calor";
    }

    /**
     * 室温变化曲线图
     */
    @PostMapping("/valveSummary")
    @ResponseBody
    public AjaxResult calorSummary(CalorRecord calorRecord)
    {
        if(calorRecord.getParams().get("beginTime").equals("") && calorRecord.getParams().get("endTime").equals("")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            calorRecord.getParams().put("beginTime", simpleDateFormat.format(new Date()));
            calorRecord.getParams().put("endTime", simpleDateFormat.format(new Date()));
        }
        List<CalorRecord> calorRecordList  = calorRecordService.selectCalorRecordList(calorRecord);
        return AjaxResult.success(calorRecordList);
    }
}
